<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Nguyễn Hồng Danh - Team 2
//Module 	: BillManager
//Chức năng	: Quản lý các bill

class Admin_BillDetailController extends BaseAdminController {
		
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
	}
	
	public function indexAction(){
		//Start ------- Khởi tạo Parameter
		$tb_Bills=new Admin_Model_Bill();
		$tb_BillDetail=new Admin_Model_BillDetail();
		$tb_BillStatus=new Admin_Model_BillStatus();
		$dateInt = new Util();
		//End ------- Khởi tạo Parameter
		
		//Start ------- Check Input Parameter
		
		  if((!isset($_GET["id"]))||(!$dateInt->is_interger($_GET["id"])))
		  {
		  		$this->_redirect("/admin/bill");
		  }
		  	$bill_id=$_GET["id"];
			$getBill=$tb_Bills->getOrderById($bill_id);
			if(count($getBill)==0)
			$this->_redirect("/admin/bill");
		  if(isset($_GET['act']))
		  {
		  	if($_GET['act']==1)
		  	{
		  		$result=0;
		  		if($getBill[0]['status_name']!="finished")
		  		{
		  		$result=$tb_Bills->cancelOrder($_GET["id"]);
		  		}
		  		else $result=2;
		  		$getBill=$tb_Bills->getOrderById($bill_id);
		  	}
		  }
		
		//End ------- Check Input Parameter
		$getBillDetail=$tb_BillDetail->getBillDetailByBillId($bill_id);
		$getAllBillStatus=$tb_BillStatus->getAllBillStatus();
		//Start ------- Truyền Parameter qua View
		$this->view->Bill=$getBill;
		$this->view->BillDetail=$getBillDetail;
		$this->view->BillStatus=$getAllBillStatus;
		$this->view->result=$result;
		//End ------- Truyền Parameter qua View
	}
	
public function updateAction()
{
		//Start ------- Khởi tạo Parameter
		$this->_helper->layout()->disableLayout();
		$tb_Bills=new Admin_Model_Bill();
		$tb_BillStatus=new Admin_Model_BillStatus();
		$dateInt = new Util();
		//End ------- Khởi tạo Parameter
		
		//Start ------- Check Input Parameter
		
		  if((!isset($_GET["id"]))||(!$dateInt->is_interger($_GET["id"])))
		  {
		  		$this->_redirect("/admin/bill");
		  }
		  	$bill_id=$_GET["id"];
			$getBill=$tb_Bills->getOrderById($bill_id);
			if(count($getBill)==0)
			$this->_redirect("/admin/bill");
			$result=0;
		  		if($getBill[0]['status_name']!="finished")
		  		{
		  			$result=$tb_Bills->updateOrder($bill_id, $_GET['status']);
		  			}
		  		else $result=2;
		  		$this->view->result=$result;
		  	$getBill=$tb_Bills->getOrderById($bill_id);
		$getAllBillStatus=$tb_BillStatus->getAllBillStatus();
		//End ------- Check Input Parameter
		$this->view->Bill=$getBill;
		$this->view->BillStatus=$getAllBillStatus;
	}
	
public function printAction(){
	$this->_helper->layout()->disableLayout();
		//Start ------- Khởi tạo Parameter
		$tb_Bills=new Admin_Model_Bill();
		$tb_BillDetail=new Admin_Model_BillDetail();
		$tb_BillStatus=new Admin_Model_BillStatus();
		$dateInt = new Util();
		//End ------- Khởi tạo Parameter
		
		//Start ------- Check Input Parameter
		
		  if((!isset($_GET["id"]))||(!$dateInt->is_interger($_GET["id"])))
		  {
		  		$this->_redirect("/admin/bill");
		  }
		  	$bill_id=$_GET["id"];
			$getBill=$tb_Bills->getOrderById($_GET["id"]);
			if(count($getBill)==0)
			$this->_redirect("/admin/bill");

		//End ------- Check Input Parameter
		$getBillDetail=$tb_BillDetail->getBillDetailByBillId($bill_id);
		$getAllBillStatus=$tb_BillStatus->getAllBillStatus();
		//Start ------- Truyền Parameter qua View
		$this->view->Bill=$getBill;
		$this->view->BillDetail=$getBillDetail;
		$this->view->BillStatus=$getAllBillStatus;
		//End ------- Truyền Parameter qua View
	}
}